package com.suxin.config;

import com.suxin.rpc.TokenRpc;
import com.suxin.rpc.domain.LoginRpcQuery;
import feign.Logger;
import feign.Request;
import feign.Retryer;
import feign.hystrix.HystrixFeign;
import feign.slf4j.Slf4jLogger;
import org.springframework.beans.factory.ObjectFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.http.HttpMessageConverters;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.cloud.openfeign.support.SpringDecoder;
import org.springframework.cloud.openfeign.support.SpringEncoder;
import org.springframework.cloud.openfeign.support.SpringMvcContract;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @author Tang
 * @classname FeignConfig
 * @description [  ]
 * @date 2021/9/15 10:48
 */
@EnableConfigurationProperties(value = {LoginRpcQuery.class})
@Configuration
public class FeignConfig {

    @Value("${rpc.loginRpc}")
    private String loginRpc;

    @Bean
    public Logger.Level feignLoggerLevel() {
        return Logger.Level.FULL;
    }

    @Bean
    public TokenRpc tokenRpc(ObjectFactory<HttpMessageConverters> converter) {
        return HystrixFeign.builder()
                .encoder(new SpringEncoder(converter))
                .decoder(new SpringDecoder(converter))
                .options(new Request.Options(1000, 5000))
                .retryer(new Retryer.Default(500, 5000, 3))
                .logger(new Slf4jLogger())
                .logLevel(Logger.Level.FULL)
                .contract(new SpringMvcContract())
                .target(TokenRpc.class, loginRpc);
    }

}